/*
 * (C) 2012 Sheng Yi
 * This program judes the valid  of a telephont number.
 */
#include <stdio.h>
#include <string.h>

int MonthDays[] = {
	31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};

int verifyIDcard(char* inIDcard);

int main()
{
	char inIDcard[100];
	int i;
	for (i = 0; i < 100; i++)
	{
		if ((inIDcard[i] = getchar()) == '\n')
		{
			inIDcard[i] = '\0';
			break;
		}
	}
	int res = verifyIDcard(inIDcard);
	switch (res)
	{
	case 0:
		printf("The IDCardNumber is valid.\n");
		break;
	case 1:
		printf("Invalid IDCardNumber: unexcept length!\n");
		break;
	case 2:
		printf("Invalid IDCardNumber: contains unexcept charactors!\n");
		break;
	case 3:
		printf("Invalid IDCardNumber: must be IDCardNumberic or x in 18th!\n");
		break;
	case 4:
		printf("Invalid IDCardNumber: unexcept year!\n");
		break;
	case 5:
		printf("Invalid IDCardNumber: unexcept month!\n");
		break;
	case 6:
		printf("Invalid IDCardNumber: unexcept day!\n");
		break;
	default:
		break;
	}

	return 0;
}

int verifyIDcard(char* inIDcard)
{
	// the length must be 18
	if (strlen(inIDcard) != 18)
		return 1;

	int i;
	for (i = 0; i < 17; i++)
	{
		// the number contains non-numberic charactors
		if (inIDcard[i] < '0' || inIDcard[i] > '9')
			return 2;
	}

	// 18th must be 'x'(lowercase) or numerics
	if ((inIDcard[17] < '0' || inIDcard[17] > '9') && (inIDcard[17] != 'x'))
		return 3;
	
	// if the birthdate valid? from 7th ~ 17th
	// if years valid? from 1900 to 2100
	int year = 0;
	for (i = 6; i < 10; i++)
	{
		year *= 10;
		year += inIDcard[i] - '0';
	}
	if (year > 2100 || year < 1900)
		return 4;

	// check if leap year
	int is_leap_year = 0;
	if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
		is_leap_year = 1;

	// check month
	int month = (inIDcard[10] - '0') * 10 + (inIDcard[11] - '0');
	if (month > 12)
		return 5;
	
	int day = (inIDcard[12] - '0') * 10 + (inIDcard[13] - '0');
	if (day > MonthDays[month-1])
		return 6;
	// leap year 29 days in Febrary, 28 if normal
	if (month == 2 && (!is_leap_year) && day == 29)
		return 6;

	return 0; // the number is OK!
}
